package user;

import book.Library;
import constant.Constant;
import utils.LibrarySingleton;
import utils.PermissionException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 87111
 * Date: 2025-08-21
 * Time: 16:54
 */
public class ProxyUser {
    private User role;
    private Library library = LibrarySingleton.createLibrary();
    public ProxyUser(User user) {
        role = user;
    }

    public User getRole() {
        return role;
    }
    //检查对象是否具有调用管理员方法的权限
    private void checkIsAdminUser(String exception) {
        if(!(role instanceof AdminUser)) {
            throw new PermissionException(exception);
        }
    }

    //上架图书
    public void addBook() {
        System.out.println("添加图书");
        checkIsAdminUser("普通用户没有权限上架图书");
        ((AdminUser)role).addBook();
    }
    //修改图书 可修改书名，作者，类别
    public void updateBook() {
        System.out.println("修改图书");
        checkIsAdminUser("普通用户没有权限上架图书");
        ((AdminUser)role).updateBook();
    }
    //下架图书
    public void removeBook() {
        System.out.println("删除图书");
        checkIsAdminUser("普通用户没有权限删除图书");
        ((AdminUser)role).removeBook();
    }
    //统计借阅次数
    public void borrowNumBook() {
        System.out.println("统计图书借阅次数");
        checkIsAdminUser("普通用户没有权限统计图书借阅次数");
        ((AdminUser)role).borrowNumBook();
    }
    //查看最受欢迎的前n本书
    public void popularBook() {
        System.out.println("查看最受欢迎的前n本书");
        checkIsAdminUser("普通用户没有权限查看最受欢迎的前n本书");
        ((AdminUser)role).popularBook();
    }
    //查看库存状态
    public void numBook() {
        System.out.println("查看库存状态");
        checkIsAdminUser("普通用户没有权限查看库存状态");
        ((AdminUser)role).numBook();
    }
    //移除上架超过一年的书
    public void removeOneBook() {
        System.out.println("移除上架超过一年的书");
        checkIsAdminUser("普通用户没有权限移除上架超过一年的书");
        ((AdminUser)role).removeOneBook();
    }

    //普通用户功能

    //检查对象是否具有调用普通用户方法的权限
    private void checkIsNormalUser(String exception) {
        if(!(role instanceof NormalUser)) {
            throw new PermissionException(exception);
        }
    }
    //借阅图书
    public void borrowBook() {
        System.out.println("借阅图书");
        checkIsNormalUser("管理员没有权限借阅图书");
        ((NormalUser)role).borrowBook();
    }
    //归还图书
    public void returnBook() {
        System.out.println("归还图书");
        checkIsNormalUser("管理员没有权限归还图书");
        ((NormalUser)role).returnBook();
    }
    //查看当前个人借阅情况
    public void viewBorrowBook() {
        System.out.println("查看当前个人借阅情况");
        checkIsNormalUser("管理员没有权限查看当前个人借阅情况");
        ((NormalUser)role).viewBorrowBook();
    }

    //不同选择对应的不同功能
    public void handleOperation(int choice) {
        if(role instanceof AdminUser) {
            //管理员操作
            switch(choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBooks();
                    break;
                case Constant.EXIT:
                    library.exit();
                    break;
                case Constant.ADD_BOOK:
                    addBook();
                    break;
                case Constant.UPDATE_BOOK:
                    updateBook();
                    break;
                case Constant.REMOVE_BOOK:
                    removeBook();
                    break;
                case Constant.BORROWED_BOOK_COUNT:
                    borrowNumBook();
                    break;
                case Constant.GENERATE_BOOK:
                    popularBook();
                    break;
                case Constant.CHECK_INVENTORY_STATUS:
                    numBook();
                    break;
                case Constant.CHECK_AND_REMOVE_OLD_BOOK:
                    removeOneBook();
                    break;
                default:
                    System.out.println("无效操作");
            }
        }else {
            switch(choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBooks();
                    break;
                case Constant.EXIT:
                    library.exit();
                    break;
                case Constant.BORROWED_BOOK:
                    borrowBook();
                    break;
                case Constant.RETURN_BOOK:
                    returnBook();
                    break;
                case Constant.VIEW_BORROW_HISTORY_BOOK:
                    viewBorrowBook();
                    break;
                default:
                    System.out.println("无效操作");
            }
        }
    }
}
